"use strict";
$(function () {
    getStulessonHtp(1, 2, fillLessons_learning);//获取学习中课程
    getStulessonHtp(1, 1, fillLessons_apply);//获取申请中课程
    getStulessonHtp(1, 3, fillLessons_learned);//获取已结束中课程
});
var tempHt=800;
function setParentIframeHeight() {
    // 子页面设置父页面iframe高度
    if (window.parent.setiframeHeight) {
        var ht = $("#container").height();
        ht = ht > tempHt ? ht : tempHt;
        tempHt=ht;
        window.parent.setiframeHeight(ht + 20);
    }
}
function cancleApply(e) {
    var temp = e.id.split("_");
    var id = temp[1];
    var phase = temp[2];
    var url = "publishlessons/" + id + "/" + phase + "/studentlist/students/" + UserID;
    $.ajax({
        url: url,
        type: "DELETE",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            if (data) {
                layer.msg(langThis("st_cancleok"));
                e.disabled = true;
            } else {
                layer.msg(langThis("st_cancleFail"));
            }
        },
        error: function (event) {
            layer.msg(langThis("st_cancleFail"));

        }
    });
}
function viewGrade(e) {

    window.parent.setScoreContent();
    location.href = "scoreManage";
}
function fillLessons_learning(data) {
    if (data.result != undefined && data.result.length > 0) {
        var lessonsHtml = "";
        for (var i in data.result) {
            var theLesson = data.result[i];
            var theLessonId = theLesson.id;
            var phase = theLesson.phase;
            lessonsHtml += "<li class=\"media lessonbottomHr\"><a class=\"media-left\" href=\"#\"><img src=\"" + theLesson.image + "\"  class=\"lessonimg\"></a><div class=\"media-body\"><h3 class=\"media-heading lessonTitle\">" + theLesson.name + "</h3><p class=\"lessontxt\">" + theLesson.description + "</p><a  role=\"button\" class=\"btn lessonBtn\" target=\"_blank\" href=\"lessonLearn?i=" + theLessonId + "&p=" + phase + "\">" + langThis("ms_goSty") + "</a><a  role=\"button\" class=\"btn applyBtn displaynone\" target=\"_blank\"  href=\"uploadhwk?i=" + theLessonId + "&p=" + phase + "\">" + langThis("ms_upHomework") + "</a><span class=\"label label-danger typepahse\">"+codePahse(phase)+"</span><span class=\"label label-info typepahse\">"+(/zh/.test(getCurrentLang()) ? theLesson.typenamecn : theLesson.typenameen)+"</span></div></li>";
        }
        $("#chooseL").hide();
        $("#lessonsUl_learning").html(lessonsHtml);
    }
    setParentIframeHeight();
}
function fillLessons_learned(data) {
    if (data.result != undefined) {
        var lessonsHtml = "";
        for (var i in data.result) {
            var theLesson = data.result[i];
            var theLessonId = theLesson.id;
            var phase = theLesson.phase;
            lessonsHtml += "<li class=\"media lessonbottomHr\"><a class=\"media-left\" href=\"#\"><img src=\"" + theLesson.image + "\"  class=\"lessonimg\"></a><div class=\"media-body\"><h3 class=\"media-heading lessonTitle\">" + theLesson.name + "</h3><p class=\"lessontxt\">" + theLesson.description + "</p><input  type=\"button\" class=\"btn lessonBtn\"  id=\"gradeBtn_" + theLessonId + "_" + phase + " onclick=\"viewGrade(this)\" value=\"" + langThis("ms_checkGrade") + "\"/><span class=\"label label-danger typepahse\">"+codePahse(phase)+"</span><span class=\"label label-info typepahse\">"+(/zh/.test(getCurrentLang()) ? theLesson.typenamecn : theLesson.typenameen)+"</span></div></li>";

        }
        $("#lessonsUl_learned").html(lessonsHtml);
    }
    setParentIframeHeight();
}
function fillLessons_apply(data) {
    var lessonsHtml = "";
    for (var i in data.result) {
        var theLesson = data.result[i];
        var theLessonId = theLesson.id;
        var phase = theLesson.phase;
        lessonsHtml += "<li class=\"media lessonbottomHr\"><a class=\"media-left\" href=\"#\"><img src=\"" + theLesson.image + "\"  class=\"lessonimg\"></a><div class=\"media-body\"><h3 class=\"media-heading lessonTitle\">" + theLesson.name + "</h3><p class=\"lessontxt\">" + theLesson.description + "</p><a  role=\"button\" class=\"btn lessonBtn\" target=\"_blank\" href=\"lessonDetail?i=" + theLessonId + "&p=" + phase + "\">" + langThis("ms_lessDetail") + "</a><input  type=\"button\" class=\"btn applyBtn\" value=\"" + langThis("ms_cancleAp") + "\" onclick=\"cancleApply(this)\" id=\"cancleAppBtn_" + theLessonId + "_" + phase + "\"/><span class=\"label label-danger typepahse\">"+codePahse(phase)+"</span><span class=\"label label-info typepahse\">"+(/zh/.test(getCurrentLang()) ? theLesson.typenamecn : theLesson.typenameen)+"</span></div></li>";

    }
    $("#lessonsUl_applying").html(lessonsHtml);
    setParentIframeHeight();
}
function codePahse(x){
 return  langThis('cm_coursePhase').replace('{p}',x);
}
function getStulessonHtp(pageIndex, modeid, successCallback) {
    var param = {
        "pagesize": 10,
        "pageindex": pageIndex,
        "modeid": modeid,
        "studentid":UserID// '9707d1d438ed44fdb39ef595ccf0bb17'//UserID
    };
    $.ajax({
        url: "publishlessons/queryresult",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(param),
        dataType: "json",
        success: successCallback,
        error: function (event) {
            layer.msg(langThis("kc_queryLessonFail"));
        }
    });
}